/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.transport.pipe;

import java.io.IOException;

import com.argot.network.TypeEndPoint;
import com.argot.network.TypeEndPointBasic;
import com.argot.network.TypeLink;
import com.colony.transport.Server;


import com.colony.concurrent.ThreadActivator;
import com.colony.mime.MimeInputStream;
import com.colony.mime.MimeOutputStream;
import com.colony.mime.MimePipe;

public class PipeServer
extends Server
{
	public PipeServer( TypeLink back, ThreadActivator pool )
	{
		super( back, pool );
	}
	
	public PipeServer( ThreadActivator pool )
	{
		super( pool );
	}
	
	protected TypeEndPoint openLink()
	{
		// Create in/out pipes
		MimePipe pipeIn = new MimePipe();
		MimePipe pipeOut = new MimePipe();
		
		// Setup the client EndPoint.
		MimeInputStream clientIn = pipeIn.inputStream();
		MimeOutputStream clientOut = pipeOut.outputStream();
		TypeEndPoint client = new TypeEndPointBasic( clientIn, clientOut );
				
		// Setup the server EndPoint
		MimeInputStream serverIn = pipeOut.inputStream();
		MimeOutputStream serverOut = pipeIn.outputStream();
		TypeEndPoint server = new TypeEndPointBasic( serverIn, serverOut );
		
		assignServerThread( server );
		
		return client;
	}
	
	protected void processFinish(TypeEndPoint request)
	{

		try
		{
			request.getInputStream().close();
		}
		catch (IOException e)
		{
			// do nothing.
		}

		try
		{
			request.getOutputStream().close();
		}
		catch (IOException e1)
		{
			// do nothing.
		}

	}

}
